package com.kun.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.rental.entity.CarrentalBrand;
import com.kun.rental.service.ICarrentalBrandService;
import com.kun.rental.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ikun
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/rental/carrentalBrand")
public class CarrentalBrandController {

    @Autowired
    private ICarrentalBrandService carrentalBrandService;

    /**
     * 分页查询
     *
     * @param start
     * @param size
     * @param carrentalBrand
     * @return
     */
    @PostMapping("/{start}/{size}")
    public Result search(@PathVariable int start,
                         @PathVariable int size,
                         @RequestBody CarrentalBrand carrentalBrand) {
        Page<CarrentalBrand> page = new Page<>(start, size);
        return Result.success().setData(carrentalBrandService.searchByPage(page, carrentalBrand));
    }

    /**
     * 新增品牌信息
     *
     * @param carrentalBrand
     * @return
     */
    @PostMapping
    public Result save(@RequestBody CarrentalBrand carrentalBrand) {
        return carrentalBrandService.save(carrentalBrand) ?
                Result.success() : Result.fail();
    }

    /**
     * 修改品牌信息
     *
     * @param carrentalBrand
     * @return
     */
    @PutMapping
    public Result update(@RequestBody CarrentalBrand carrentalBrand) {
        return carrentalBrandService.updateById(carrentalBrand) ?
                Result.success() : Result.fail();
    }


    /**
     * 删除品牌信息
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids) {
        List<Integer> list = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .toList();
        return carrentalBrandService.removeByIds(list) ?
                Result.success() : Result.fail();
    }

    /**
     * 根据厂商id查询品牌信息
     * @param makerId
     * @return
     */
    @GetMapping("/selectByMakerId/{makerId}")
    public Result selectByMakerId(@PathVariable Integer makerId){
        return Result.success().setData(carrentalBrandService.selectByMakerId(makerId));
    }

}

